home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / dist / gdb / remote.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-02  |  23.6 KB  |  1,045 lines

  1. /* Remote target communications for serial-line targets in custom GDB protocol
  2.    Copyright 1988, 1991, 1992 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Remote communication protocol.
  21.    All values are encoded in ascii hex digits.
  22.  
  23.     Request        Packet
  24.  
  25.     read registers  g
  26.     reply        XX....X        Each byte of register data
  27.                     is described by two hex digits.
  28.                     Registers are in the internal order
  29.                     for GDB, and the bytes in a register
  30.                     are in the same order the machine uses.
  31.             or ENN        for an error.
  32.  
  33.     write regs    GXX..XX        Each byte of register data
  34.                     is described by two hex digits.
  35.     reply        OK        for success
  36.             ENN        for an error
  37.  
  38.     read mem    mAA..AA,LLLL    AA..AA is address, LLLL is length.
  39.     reply        XX..XX        XX..XX is mem contents
  40.             or ENN        NN is errno
  41.  
  42.     write mem    MAA..AA,LLLL:XX..XX
  43.                     AA..AA is address,
  44.                     LLLL is number of bytes,
  45.                     XX..XX is data
  46.     reply        OK        for success
  47.             ENN        for an error
  48.  
  49.     cont        cAA..AA        AA..AA is address to resume
  50.                     If AA..AA is omitted,
  51.                     resume at same address.
  52.  
  53.     step        sAA..AA        AA..AA is address to resume
  54.                     If AA..AA is omitted,
  55.                     resume at same address.
  56.  
  57.     last signal     ?               Reply the current reason for stopping.
  58.                                         This is the same reply as is generated
  59.                     for step or cont : SAA where AA is the
  60.                     signal number.
  61.  
  62.     There is no immediate reply to step or cont.
  63.     The reply comes when the machine stops.
  64.     It is        SAA        AA is the "signal number"
  65.  
  66.     or...        TAAPPPPPPPPFFFFFFFF
  67.                     where AA is the signal number,
  68.                     PPPPPPPP is the PC (PC_REGNUM), and
  69.                     FFFFFFFF is the frame ptr (FP_REGNUM).
  70.  
  71.     kill req    k
  72. */
  73.  
  74. #include "defs.h"
  75. #include <string.h>
  76. #include <fcntl.h>
  77. #include "frame.h"
  78. #include "inferior.h"
  79. #include "target.h"
  80. #include "wait.h"
  81. #include "terminal.h"
  82.  
  83. #ifdef USG
  84. #include <sys/types.h>
  85. #endif
  86.  
  87. #include <signal.h>
  88.  
  89. /* Prototypes for local functions */
  90.  
  91. static void
  92. remote_write_bytes PARAMS ((CORE_ADDR, char *, int));
  93.  
  94. static void
  95. remote_read_bytes PARAMS ((CORE_ADDR, char *, int));
  96.  
  97. static void
  98. remote_files_info PARAMS ((struct target_ops *));
  99.  
  100. static int
  101. remote_xfer_memory PARAMS ((CORE_ADDR, char *, int, int, struct target_ops *));
  102.  
  103. static void 
  104. remote_prepare_to_store PARAMS ((void));
  105.  
  106. static void
  107. remote_fetch_registers PARAMS ((int));
  108.  
  109. static void
  110. remote_resume PARAMS ((int, int));
  111.  
  112. static void
  113. remote_open PARAMS ((char *, int));
  114.  
  115. static void
  116. remote_close PARAMS ((int));
  117.  
  118. static void
  119. remote_store_registers PARAMS ((int));
  120.  
  121. static void
  122. getpkt PARAMS ((char *));
  123.  
  124. static void
  125. putpkt PARAMS ((char *));
  126.  
  127. static void
  128. remote_send PARAMS ((char *));
  129.  
  130. static int
  131. readchar PARAMS ((void));
  132.  
  133. static int
  134. remote_wait PARAMS ((WAITTYPE *));
  135.  
  136. static int
  137. tohex PARAMS ((int));
  138.  
  139. static int
  140. fromhex PARAMS ((int));
  141.  
  142. static void
  143. remote_detach PARAMS ((char *, int));
  144.  
  145.  
  146. extern struct target_ops remote_ops;    /* Forward decl */
  147.  
  148. static int kiodebug = 0;
  149. static int timeout = 5;
  150.  
  151. #if 0
  152. int icache;
  153. #endif
  154.  
  155. /* Descriptor for I/O to remote machine.  Initialize it to -1 so that
  156.    remote_open knows that we don't have a file open when the program
  157.    starts.  */
  158. int remote_desc = -1;
  159.  
  160. #define    PBUFSIZ    1024
  161.  
  162. /* Maximum number of bytes to read/write at once.  The value here
  163.    is chosen to fill up a packet (the headers account for the 32).  */
  164. #define MAXBUFBYTES ((PBUFSIZ-32)/2)
  165.  
  166. /* Round up PBUFSIZ to hold all the registers, at least.  */
  167. #if REGISTER_BYTES > MAXBUFBYTES
  168. #undef    PBUFSIZ
  169. #define    PBUFSIZ    (REGISTER_BYTES * 2 + 32)
  170. #endif
  171.  
  172. /* Called when SIGALRM signal sent due to alarm() timeout.  */
  173. #ifndef HAVE_TERMIO
  174. void
  175. remote_timer ()
  176. {
  177.   if (kiodebug)
  178.     printf ("remote_timer called\n");
  179.  
  180.   alarm (timeout);
  181. }
  182. #endif
  183.  
  184. /* Clean up connection to a remote debugger.  */
  185.  
  186. /* ARGSUSED */
  187. static void
  188. remote_close (quitting)
  189.      int quitting;
  190. {
  191.   if (remote_desc >= 0)
  192.     close (remote_desc);
  193.   remote_desc = -1;
  194. }
  195.  
  196. /* Translate baud rates from integers to damn B_codes.  Unix should
  197.    have outgrown this crap years ago, but even POSIX wouldn't buck it.  */
  198.  
  199. #ifndef B19200
  200. #define B19200 EXTA
  201. #endif
  202. #ifndef B38400
  203. #define B38400 EXTB
  204. #endif
  205.  
  206. static struct {int rate, damn_b;} baudtab[] = {
  207.     {0, B0},
  208.     {50, B50},
  209.     {75, B75},
  210.     {110, B110},
  211.     {134, B134},
  212.     {150, B150},
  213.     {200, B200},
  214.     {300, B300},
  215.     {600, B600},
  216.     {1200, B1200},
  217.     {1800, B1800},
  218.     {2400, B2400},
  219.     {4800, B4800},
  220.     {9600, B9600},
  221.     {19200, B19200},
  222.     {38400, B38400},
  223.     {-1, -1},
  224. };
  225.  
  226. static int
  227. damn_b (rate)
  228.      int rate;
  229. {
  230.   int i;
  231.  
  232.   for (i = 0; baudtab[i].rate != -1; i++)
  233.     if (rate == baudtab[i].rate) return baudtab[i].damn_b;
  234.   return B38400;    /* Random */
  235. }
  236.  
  237. /* Open a connection to a remote debugger.
  238.    NAME is the filename used for communication.  */
  239.  
  240. static void
  241. remote_open (name, from_tty)
  242.      char *name;
  243.      int from_tty;
  244. {
  245.   TERMINAL sg;
  246.   int a_rate, b_rate = 0;
  247.   int baudrate_set = 0;
  248.  
  249.   if (name == 0)
  250.     error (
  251. "To open a remote debug connection, you need to specify what serial\n\
  252. device is attached to the remote system (e.g. /dev/ttya).");
  253.  
  254.   target_preopen (from_tty);
  255.  
  256.   remote_close (0);
  257.  
  258. #if 0
  259.   dcache_init ();
  260. #endif
  261.  
  262.   remote_desc = open (name, O_RDWR);
  263.   if (remote_desc < 0)
  264.     perror_with_name (name);
  265.  
  266.   if (baud_rate)
  267.     {
  268.       if (1 != sscanf (baud_rate, "%d ", &a_rate))
  269.     {
  270.       b_rate = damn_b (a_rate);
  271.       baudrate_set = 1;
  272.     }
  273.     }
  274.  
  275.   ioctl (remote_desc, TIOCGETP, &sg);
  276. #ifdef HAVE_TERMIO
  277.   sg.c_cc[VMIN] = 0;        /* read with timeout.  */
  278.   sg.c_cc[VTIME] = timeout * 10;
  279.   sg.c_lflag &= ~(ICANON | ECHO);
  280.   sg.c_cflag &= ~PARENB;    /* No parity */
  281.   sg.c_cflag |= CS8;        /* 8-bit path */
  282.   if (baudrate_set)
  283.     sg.c_cflag = (sg.c_cflag & ~CBAUD) | b_rate;
  284. #else
  285.   sg.sg_flags |= RAW | ANYP;
  286.   sg.sg_flags &= ~ECHO;
  287.   if (baudrate_set)
  288.     {
  289.       sg.sg_ispeed = b_rate;
  290.       sg.sg_ospeed = b_rate;
  291.     }
  292. #endif
  293.   ioctl (remote_desc, TIOCSETP, &sg);
  294.  
  295.   if (from_tty)
  296.     printf ("Remote debugging using %s\n", name);
  297.   push_target (&remote_ops);    /* Switch to using remote target now */
  298.  
  299. #ifndef HAVE_TERMIO
  300. #ifndef NO_SIGINTERRUPT
  301.   /* Cause SIGALRM's to make reads fail.  */
  302.   if (siginterrupt (SIGALRM, 1) != 0)
  303.     perror ("remote_open: error in siginterrupt");
  304. #endif
  305.  
  306.   /* Set up read timeout timer.  */
  307.   if ((void (*)()) signal (SIGALRM, remote_timer) == (void (*)()) -1)
  308.     perror ("remote_open: error in signal");
  309. #endif
  310.  
  311.   /* Ack any packet which the remote side has already sent.  */
  312.   write (remote_desc, "+\r", 2);
  313.   putpkt ("?");            /* initiate a query from remote machine */
  314.  
  315.   start_remote ();        /* Initialize gdb process mechanisms */
  316. }
  317.  
  318. /* remote_detach()
  319.    takes a program previously attached to and detaches it.
  320.    We better not have left any breakpoints
  321.    in the program or it'll die when it hits one.
  322.    Close the open connection to the remote debugger.
  323.    Use this when you want to detach and do something else
  324.    with your gdb.  */
  325.  
  326. static void
  327. remote_detach (args, from_tty)
  328.      char *args;
  329.      int from_tty;
  330. {
  331.   if (args)
  332.     error ("Argument given to \"detach\" when remotely debugging.");
  333.   
  334.   pop_target ();
  335.   if (from_tty)
  336.     printf ("Ending remote debugging.\n");
  337. }
  338.  
  339. /* Convert hex digit A to a number.  */
  340.  
  341. static int
  342. fromhex (a)
  343.      int a;
  344. {
  345.   if (a >= '0' && a <= '9')
  346.     return a - '0';
  347.   else if (a >= 'a' && a <= 'f')
  348.     return a - 'a' + 10;
  349.   else
  350.     error ("Reply contains invalid hex digit");
  351.   return -1;
  352. }
  353.  
  354. /* Convert number NIB to a hex digit.  */
  355.  
  356. static int
  357. tohex (nib)
  358.      int nib;
  359. {
  360.   if (nib < 10)
  361.     return '0'+nib;
  362.   else
  363.     return 'a'+nib-10;
  364. }
  365.  
  366. /* Tell the remote machine to resume.  */
  367.  
  368. static void
  369. remote_resume (step, siggnal)
  370.      int step, siggnal;
  371. {
  372.   char buf[PBUFSIZ];
  373.  
  374.   if (siggnal)
  375.     error ("Can't send signals to a remote system.  Try `handle %d ignore'.",
  376.        siggnal);
  377.  
  378. #if 0
  379.   dcache_flush ();
  380. #endif
  381.  
  382.   strcpy (buf, step ? "s": "c");
  383.  
  384.   putpkt (buf);
  385. }
  386.  
  387. /* Send ^C to target to halt it.  Target will respond, and send us a
  388.    packet.  */
  389.  
  390. void remote_interrupt()
  391. {
  392.   
  393.   if (kiodebug)
  394.     printf ("remote_interrupt called\n");
  395.  
  396.   write (remote_desc, "\003", 1);    /* Send a ^C */
  397. }
  398.  
  399.  
  400. /* Wait until the remote machine stops, then return,
  401.    storing status in STATUS just as `wait' would.
  402.    Returns "pid" (though it's not clear what, if anything, that
  403.    means in the case of this target).  */
  404.  
  405. static int
  406. remote_wait (status)
  407.      WAITTYPE *status;
  408. {
  409.   unsigned char buf[PBUFSIZ];
  410.   void (*ofunc)();
  411.   unsigned char *p;
  412.   int i;
  413.   char regs[REGISTER_RAW_SIZE (PC_REGNUM) + REGISTER_RAW_SIZE (FP_REGNUM)];
  414.  
  415.   WSETEXIT ((*status), 0);
  416.  
  417.   ofunc = signal (SIGINT, remote_interrupt);
  418.   getpkt ((char *) buf);
  419.   signal (SIGINT, ofunc);
  420.  
  421.   if (buf[0] == 'E')
  422.     error ("Remote failure reply: %s", buf);
  423.   if (buf[0] == 'T')
  424.     {
  425.       /* Expedited reply, containing Signal, PC, and FP.  */
  426.       p = &buf[3];        /* after Txx */
  427.       for (i = 0; i < sizeof (regs); i++)
  428.     {
  429.       if (p[0] == 0 || p[1] == 0)
  430.         error ("Remote reply is too short: %s", buf);
  431.       regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
  432.       p += 2;
  433.     }
  434.       supply_register (PC_REGNUM, ®s[0]);
  435.       supply_register (FP_REGNUM, ®s[REGISTER_RAW_SIZE (PC_REGNUM)]);
  436.     }
  437.   else if (buf[0] != 'S')
  438.     error ("Invalid remote reply: %s", buf);
  439.  
  440.   WSETSTOP ((*status), (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))));
  441.  
  442.   return 0;
  443. }
  444.  
  445. /* Read the remote registers into the block REGS.  */
  446. /* Currently we just read all the registers, so we don't use regno.  */
  447. /* ARGSUSED */
  448. static void
  449. remote_fetch_registers (regno)
  450.      int regno;
  451. {
  452.   char buf[PBUFSIZ];
  453.   int i;
  454.   char *p;
  455.   char regs[REGISTER_BYTES];
  456.  
  457.   sprintf (buf, "g");
  458.   remote_send (buf);
  459.  
  460.   /* Reply describes registers byte by byte, each byte encoded as two
  461.      hex characters.  Suck them all up, then supply them to the
  462.      register cacheing/storage mechanism.  */
  463.  
  464.   p = buf;
  465.   for (i = 0; i < REGISTER_BYTES; i++)
  466.     {
  467.       if (p[0] == 0 || p[1] == 0)
  468.     error ("Remote reply is too short: %s", buf);
  469.       regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
  470.       p += 2;
  471.     }
  472.   for (i = 0; i < NUM_REGS; i++)
  473.     supply_register (i, ®s[REGISTER_BYTE(i)]);
  474. }
  475.  
  476. /* Prepare to store registers.  Since we send them all, we have to
  477.    read out the ones we don't want to change first.  */
  478.  
  479. static void 
  480. remote_prepare_to_store ()
  481. {
  482.   remote_fetch_registers (-1);
  483. }
  484.  
  485. /* Store the remote registers from the contents of the block REGISTERS. 
  486.    FIXME, eventually just store one register if that's all that is needed.  */
  487.  
  488. /* ARGSUSED */
  489. static void
  490. remote_store_registers (regno)
  491.      int regno;
  492. {
  493.   char buf[PBUFSIZ];
  494.   int i;
  495.   char *p;
  496.  
  497.   buf[0] = 'G';
  498.   
  499.   /* Command describes registers byte by byte,
  500.      each byte encoded as two hex characters.  */
  501.  
  502.   p = buf + 1;
  503.   for (i = 0; i < REGISTER_BYTES; i++)
  504.     {
  505.       *p++ = tohex ((registers[i] >> 4) & 0xf);
  506.       *p++ = tohex (registers[i] & 0xf);
  507.     }
  508.   *p = '\0';
  509.  
  510.   remote_send (buf);
  511. }
  512.  
  513. #if 0
  514. /* Read a word from remote address ADDR and return it.
  515.    This goes through the data cache.  */
  516.  
  517. int
  518. remote_fetch_word (addr)
  519.      CORE_ADDR addr;
  520. {
  521.   if (icache)
  522.     {
  523.       extern CORE_ADDR text_start, text_end;
  524.  
  525.       if (addr >= text_start && addr < text_end)
  526.     {
  527.       int buffer;
  528.       xfer_core_file (addr, &buffer, sizeof (int));
  529.       return buffer;
  530.     }
  531.     }
  532.   return dcache_fetch (addr);
  533. }
  534.  
  535. /* Write a word WORD into remote address ADDR.
  536.    This goes through the data cache.  */
  537.  
  538. void
  539. remote_store_word (addr, word)
  540.      CORE_ADDR addr;
  541.      int word;
  542. {
  543.   dcache_poke (addr, word);
  544. }
  545. #endif /* 0 */
  546.  
  547. /* Write memory data directly to the remote machine.
  548.    This does not inform the data cache; the data cache uses this.
  549.    MEMADDR is the address in the remote memory space.
  550.    MYADDR is the address of the buffer in our space.
  551.    LEN is the number of bytes.  */
  552.  
  553. static void
  554. remote_write_bytes (memaddr, myaddr, len)
  555.      CORE_ADDR memaddr;
  556.      char *myaddr;
  557.      int len;
  558. {
  559.   char buf[PBUFSIZ];
  560.   int i;
  561.   char *p;
  562.  
  563.   if (len > PBUFSIZ / 2 - 20)
  564.     abort ();
  565.  
  566.   sprintf (buf, "M%x,%x:", memaddr, len);
  567.  
  568.   /* We send target system values byte by byte, in increasing byte addresses,
  569.      each byte encoded as two hex characters.  */
  570.  
  571.   p = buf + strlen (buf);
  572.   for (i = 0; i < len; i++)
  573.     {
  574.       *p++ = tohex ((myaddr[i] >> 4) & 0xf);
  575.       *p++ = tohex (myaddr[i] & 0xf);
  576.     }
  577.   *p = '\0';
  578.  
  579.   remote_send (buf);
  580. }
  581.  
  582. /* Read memory data directly from the remote machine.
  583.    This does not use the data cache; the data cache uses this.
  584.    MEMADDR is the address in the remote memory space.
  585.    MYADDR is the address of the buffer in our space.
  586.    LEN is the number of bytes.  */
  587.  
  588. static void
  589. remote_read_bytes (memaddr, myaddr, len)
  590.      CORE_ADDR memaddr;
  591.      char *myaddr;
  592.      int len;
  593. {
  594.   char buf[PBUFSIZ];
  595.   int i;
  596.   char *p;
  597.  
  598.   if (len > PBUFSIZ / 2 - 1)
  599.     abort ();
  600.  
  601.   sprintf (buf, "m%x,%x", memaddr, len);
  602.   remote_send (buf);
  603.  
  604.   /* Reply describes memory byte by byte,
  605.      each byte encoded as two hex characters.  */
  606.  
  607.   p = buf;
  608.   for (i = 0; i < len; i++)
  609.     {
  610.       if (p[0] == 0 || p[1] == 0)
  611.     error ("Remote reply is too short: %s", buf);
  612.       myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
  613.       p += 2;
  614.     }
  615. }
  616.  
  617. /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
  618.    to or from debugger address MYADDR.  Write to inferior if SHOULD_WRITE is
  619.    nonzero.  Returns length of data written or read; 0 for error.  */
  620.  
  621. /* ARGSUSED */
  622. static int
  623. remote_xfer_memory(memaddr, myaddr, len, should_write, target)
  624.      CORE_ADDR memaddr;
  625.      char *myaddr;
  626.      int len;
  627.      int should_write;
  628.      struct target_ops *target;            /* ignored */
  629. {
  630.   int origlen = len;
  631.   int xfersize;
  632.   while (len > 0)
  633.     {
  634.       if (len > MAXBUFBYTES)
  635.     xfersize = MAXBUFBYTES;
  636.       else
  637.     xfersize = len;
  638.  
  639.       if (should_write)
  640.         remote_write_bytes(memaddr, myaddr, xfersize);
  641.       else
  642.     remote_read_bytes (memaddr, myaddr, xfersize);
  643.       memaddr += xfersize;
  644.       myaddr  += xfersize;
  645.       len     -= xfersize;
  646.     }
  647.   return origlen; /* no error possible */
  648. }
  649.  
  650. static void
  651. remote_files_info (ignore)
  652. struct target_ops *ignore;
  653. {
  654.   printf ("Debugging a target over a serial line.\n");
  655. }
  656.  
  657. /*
  658.  
  659. A debug packet whose contents are <data>
  660. is encapsulated for transmission in the form:
  661.  
  662.     $ <data> # CSUM1 CSUM2
  663.  
  664.     <data> must be ASCII alphanumeric and cannot include characters
  665.     '$' or '#'
  666.  
  667.     CSUM1 and CSUM2 are ascii hex representation of an 8-bit 
  668.     checksum of <data>, the most significant nibble is sent first.
  669.     the hex digits 0-9,a-f are used.
  670.  
  671. Receiver responds with:
  672.  
  673.     +    - if CSUM is correct and ready for next packet
  674.     -    - if CSUM is incorrect
  675.  
  676. */
  677.  
  678. /* Read a single character from the remote end.
  679.    (If supported, we actually read many characters and buffer them up.)  */
  680.  
  681. static int
  682. readchar ()
  683. {
  684.   static int inbuf_index, inbuf_count;
  685. #define    INBUFSIZE    PBUFSIZ
  686.   static char inbuf[INBUFSIZE];
  687.  
  688.   if (inbuf_index >= inbuf_count)
  689.     {
  690.       /* Time to do another read... */
  691.       inbuf_index = 0;
  692.       inbuf_count = 0;
  693.       inbuf[0] = 0;        /* Just in case */
  694. #ifdef HAVE_TERMIO
  695.       /* termio does the timeout for us.  */
  696.       inbuf_count = read (remote_desc, inbuf, INBUFSIZE);
  697. #else
  698.       alarm (timeout);
  699.       inbuf_count = read (remote_desc, inbuf, INBUFSIZE);
  700.       alarm (0);
  701. #endif
  702.     }
  703.  
  704.   /* Just return the next character from the buffer.  */
  705.   return inbuf[inbuf_index++] & 0x7f;
  706. }
  707.  
  708. /* Send the command in BUF to the remote machine,
  709.    and read the reply into BUF.
  710.    Report an error if we get an error reply.  */
  711.  
  712. static void
  713. remote_send (buf)
  714.      char *buf;
  715. {
  716.  
  717.   putpkt (buf);
  718.   getpkt (buf);
  719.  
  720.   if (buf[0] == 'E')
  721.     error ("Remote failure reply: %s", buf);
  722. }
  723.  
  724. /* Send a packet to the remote machine, with error checking.
  725.    The data of the packet is in BUF.  */
  726.  
  727. static void
  728. putpkt (buf)
  729.      char *buf;
  730. {
  731.   int i;
  732.   unsigned char csum = 0;
  733.   char buf2[PBUFSIZ];
  734.   int cnt = strlen (buf);
  735.   char ch;
  736.   char *p;
  737.  
  738.   /* Copy the packet into buffer BUF2, encapsulating it
  739.      and giving it a checksum.  */
  740.  
  741.   if (cnt > sizeof(buf2) - 5)        /* Prosanity check */
  742.     abort();
  743.  
  744.   p = buf2;
  745.   *p++ = '$';
  746.  
  747.   for (i = 0; i < cnt; i++)
  748.     {
  749.       csum += buf[i];
  750.       *p++ = buf[i];
  751.     }
  752.   *p++ = '#';
  753.   *p++ = tohex ((csum >> 4) & 0xf);
  754.   *p++ = tohex (csum & 0xf);
  755.  
  756.   /* Send it over and over until we get a positive ack.  */
  757.  
  758.   do {
  759.     if (kiodebug)
  760.       {
  761.     *p = '\0';
  762.     printf ("Sending packet: %s...", buf2);  fflush(stdout);
  763.       }
  764.     write (remote_desc, buf2, p - buf2);
  765.  
  766.     /* read until either a timeout occurs (\0) or '+' is read */
  767.     do {
  768.       ch = readchar ();
  769.       if (kiodebug) {
  770.     if (ch == '+')
  771.       printf("Ack\n");
  772.     else
  773.       printf ("%02X%c ", ch&0xFF, ch);
  774.       }
  775.     } while ((ch != '+') && (ch != '\0'));
  776.   } while (ch != '+');
  777. }
  778.  
  779. /* Read a packet from the remote machine, with error checking,
  780.    and store it in BUF.  */
  781.  
  782. static void
  783. getpkt (buf)
  784.      char *buf;
  785. {
  786.   char *bp;
  787.   unsigned char csum;
  788.   int c;
  789.   unsigned char c1, c2;
  790.  
  791. #if 0
  792.   /* Sorry, this will cause all hell to break loose, i.e. we'll end
  793.      up in the command loop with an inferior, but (at least if this
  794.      happens in remote_wait or some such place) without a current_frame,
  795.      having set up prev_* in wait_for_inferior, etc.
  796.  
  797.      If it is necessary to have such an "emergency exit", seems like
  798.      the only plausible thing to do is to say the inferior died, and
  799.      make the user reattach if they want to.  Perhaps with a prompt
  800.      asking for confirmation.  */
  801.  
  802.   /* allow immediate quit while reading from device, it could be hung */
  803.   immediate_quit++;
  804. #endif /* 0 */
  805.  
  806.   while (1)
  807.     {
  808.       /* Force csum to be zero here because of possible error retry.  */
  809.       csum = 0;
  810.       
  811.       while ((c = readchar()) != '$');
  812.  
  813.       bp = buf;
  814.       while (1)
  815.     {
  816.       c = readchar ();
  817.       if (c == '#')
  818.         break;
  819.       *bp++ = c;
  820.       csum += c;
  821.     }
  822.       *bp = 0;
  823.  
  824.       c1 = fromhex (readchar ());
  825.       c2 = fromhex (readchar ());
  826.       if ((csum & 0xff) == (c1 << 4) + c2)
  827.     break;
  828.       printf ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
  829.           (c1 << 4) + c2, csum & 0xff, buf);
  830.       write (remote_desc, "-", 1);
  831.     }
  832.  
  833. #if 0
  834.   immediate_quit--;
  835. #endif
  836.  
  837.   write (remote_desc, "+", 1);
  838.  
  839.   if (kiodebug)
  840.     fprintf (stderr,"Packet received: %s\n", buf);
  841. }
  842.  
  843. /* The data cache leads to incorrect results because it doesn't know about
  844.    volatile variables, thus making it impossible to debug functions which
  845.    use hardware registers.  Therefore it is #if 0'd out.  Effect on
  846.    performance is some, for backtraces of functions with a few
  847.    arguments each.  For functions with many arguments, the stack
  848.    frames don't fit in the cache blocks, which makes the cache less
  849.    helpful.  Disabling the cache is a big performance win for fetching
  850.    large structures, because the cache code fetched data in 16-byte
  851.    chunks.  */
  852. #if 0
  853. /* The data cache records all the data read from the remote machine
  854.    since the last time it stopped.
  855.  
  856.    Each cache block holds 16 bytes of data
  857.    starting at a multiple-of-16 address.  */
  858.  
  859. #define DCACHE_SIZE 64        /* Number of cache blocks */
  860.  
  861. struct dcache_block {
  862.     struct dcache_block *next, *last;
  863.     unsigned int addr;    /* Address for which data is recorded.  */
  864.     int data[4];
  865. };
  866.  
  867. struct dcache_block dcache_free, dcache_valid;
  868.  
  869. /* Free all the data cache blocks, thus discarding all cached data.  */ 
  870.  
  871. static void
  872. dcache_flush ()
  873. {
  874.   register struct dcache_block *db;
  875.  
  876.   while ((db = dcache_valid.next) != &dcache_valid)
  877.     {
  878.       remque (db);
  879.       insque (db, &dcache_free);
  880.     }
  881. }
  882.  
  883. /*
  884.  * If addr is present in the dcache, return the address of the block 
  885.  * containing it.
  886.  */
  887.  
  888. struct dcache_block *
  889. dcache_hit (addr)
  890. {
  891.   register struct dcache_block *db;
  892.  
  893.   if (addr & 3)
  894.     abort ();
  895.  
  896.   /* Search all cache blocks for one that is at this address.  */
  897.   db = dcache_valid.next;
  898.   while (db != &dcache_valid)
  899.     {
  900.       if ((addr & 0xfffffff0) == db->addr)
  901.     return db;
  902.       db = db->next;
  903.     }
  904.   return NULL;
  905. }
  906.  
  907. /*  Return the int data at address ADDR in dcache block DC.  */
  908.  
  909. int
  910. dcache_value (db, addr)
  911.      struct dcache_block *db;
  912.      unsigned int addr;
  913. {
  914.   if (addr & 3)
  915.     abort ();
  916.   return (db->data[(addr>>2)&3]);
  917. }
  918.  
  919. /* Get a free cache block, put it on the valid list,
  920.    and return its address.  The caller should store into the block
  921.    the address and data that it describes.  */
  922.  
  923. struct dcache_block *
  924. dcache_alloc ()
  925. {
  926.   register struct dcache_block *db;
  927.  
  928.   if ((db = dcache_free.next) == &dcache_free)
  929.     /* If we can't get one from the free list, take last valid */
  930.     db = dcache_valid.last;
  931.  
  932.   remque (db);
  933.   insque (db, &dcache_valid);
  934.   return (db);
  935. }
  936.  
  937. /* Return the contents of the word at address ADDR in the remote machine,
  938.    using the data cache.  */
  939.  
  940. int
  941. dcache_fetch (addr)
  942.      CORE_ADDR addr;
  943. {
  944.   register struct dcache_block *db;
  945.  
  946.   db = dcache_hit (addr);
  947.   if (db == 0)
  948.     {
  949.       db = dcache_alloc ();
  950.       remote_read_bytes (addr & ~0xf, db->data, 16);
  951.       db->addr = addr & ~0xf;
  952.     }
  953.   return (dcache_value (db, addr));
  954. }
  955.  
  956. /* Write the word at ADDR both in the data cache and in the remote machine.  */
  957.  
  958. dcache_poke (addr, data)
  959.      CORE_ADDR addr;
  960.      int data;
  961. {
  962.   register struct dcache_block *db;
  963.  
  964.   /* First make sure the word is IN the cache.  DB is its cache block.  */
  965.   db = dcache_hit (addr);
  966.   if (db == 0)
  967.     {
  968.       db = dcache_alloc ();
  969.       remote_read_bytes (addr & ~0xf, db->data, 16);
  970.       db->addr = addr & ~0xf;
  971.     }
  972.  
  973.   /* Modify the word in the cache.  */
  974.   db->data[(addr>>2)&3] = data;
  975.  
  976.   /* Send the changed word.  */
  977.   remote_write_bytes (addr, &data, 4);
  978. }
  979.  
  980. /* Initialize the data cache.  */
  981.  
  982. dcache_init ()
  983. {
  984.   register i;
  985.   register struct dcache_block *db;
  986.  
  987.   db = (struct dcache_block *) xmalloc (sizeof (struct dcache_block) * 
  988.                     DCACHE_SIZE);
  989.   dcache_free.next = dcache_free.last = &dcache_free;
  990.   dcache_valid.next = dcache_valid.last = &dcache_valid;
  991.   for (i=0;i<DCACHE_SIZE;i++,db++)
  992.     insque (db, &dcache_free);
  993. }
  994. #endif /* 0 */
  995.  
  996. /* Define the target subroutine names */
  997.  
  998. struct target_ops remote_ops = {
  999.   "remote",            /* to_shortname */
  1000.   "Remote serial target in gdb-specific protocol",    /* to_longname */
  1001.   "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
  1002. Specify the serial device it is connected to (e.g. /dev/ttya).",  /* to_doc */
  1003.   remote_open,            /* to_open */
  1004.   remote_close,            /* to_close */
  1005.   NULL,                /* to_attach */
  1006.   remote_detach,        /* to_detach */
  1007.   remote_resume,        /* to_resume */
  1008.   remote_wait,            /* to_wait */
  1009.   remote_fetch_registers,    /* to_fetch_registers */
  1010.   remote_store_registers,    /* to_store_registers */
  1011.   remote_prepare_to_store,    /* to_prepare_to_store */
  1012.   NULL,                /* to_convert_to_virtual */
  1013.   NULL,                /* to_convert_from_virtual */
  1014.   remote_xfer_memory,        /* to_xfer_memory */
  1015.   remote_files_info,        /* to_files_info */
  1016.   NULL,                /* to_insert_breakpoint */
  1017.   NULL,                /* to_remove_breakpoint */
  1018.   NULL,                /* to_terminal_init */
  1019.   NULL,                /* to_terminal_inferior */
  1020.   NULL,                /* to_terminal_ours_for_output */
  1021.   NULL,                /* to_terminal_ours */
  1022.   NULL,                /* to_terminal_info */
  1023.   NULL,                /* to_kill */
  1024.   NULL,                /* to_load */
  1025.   NULL,                /* to_lookup_symbol */
  1026.   NULL,                /* to_create_inferior */
  1027.   NULL,                /* to_mourn_inferior */
  1028.   process_stratum,        /* to_stratum */
  1029.   NULL,                /* to_next */
  1030.   1,                /* to_has_all_memory */
  1031.   1,                /* to_has_memory */
  1032.   1,                /* to_has_stack */
  1033.   1,                /* to_has_registers */
  1034.   1,                /* to_has_execution */
  1035.   NULL,                /* sections */
  1036.   NULL,                /* sections_end */
  1037.   OPS_MAGIC            /* to_magic */
  1038. };
  1039.  
  1040. void
  1041. _initialize_remote ()
  1042. {
  1043.   add_target (&remote_ops);
  1044. }
  1045.